/*
 * WindowElement.h
 *
 *       */

#ifndef WINDOWELEMENT_H_
#define WINDOWELEMENT_H_

#include <iostream>
#include <cstring>
#include <fstream>
#include <string>
#include "Queue.h"
#include "pthread.h"
#include "Table.h"
#include "Split.h"
#include <cstring>
#include "Cache.h"
#include "stdlib.h"
#include "unistd.h"
#include "time.h"
#include <gtk/gtk.h>
#include "X11/Xlib.h";
#include "WindowElement.h"

class windowelement{
public:
	split s;
	string content;
	string req;
	void createwindowx(){
		Display *d;
				Window w;
				XEvent e;
				string printmsg = "\n"+req + "\n" + content+"\n";
				char *str;
				str = new char[printmsg.size() + 1];
				strcpy(str, printmsg.c_str());

				int s;
				d = XOpenDisplay(NULL);
				if (d == NULL) {
					fprintf(stderr, "Cannot open display\n");
					exit(1);
				}
				s = DefaultScreen(d);
				w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 500, 500, 1,
						BlackPixel(d, s), WhitePixel(d, s));
				XSelectInput(d, w, ExposureMask | KeyPressMask);
				XMapWindow(d, w);
				while (1) {
					XNextEvent(d, &e);
					if (e.type == Expose) {
						XFillRectangle(d, w, DefaultGC(d, s), 20, 20, 10, 10);
						while(1){
							char *cstr;
							cstr = new char[printmsg.size() + 1];
												strcpy(cstr, printmsg.c_str());
												char *p;
												p = strtok(cstr, "\n");
												int x=10;
												int y=50;
												while (p != NULL) {
													XDrawString(d, w, DefaultGC(d, s), x=x+10,y=y+10, p, strlen(p));
													p = strtok(NULL, "\n");
												}
												break;

						}
					}
					if (e.type == KeyPress)
						break;
				}
				XCloseDisplay(d);
	}
	void createwindow(){
		cout<<"enter create window routine";
		static GtkWidget *window = NULL;
				  GtkWidget *hbox;
				  GtkWidget *vbox;
				  GtkWidget *frame;
				  GtkWidget *label;
				  cout<<"creating a window"<<s.getFilename()<<","<<s.getIp()<<endl;
				  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
				    g_signal_connect (window, "destroy",
				  		    G_CALLBACK (gtk_main_quit),
				  		    NULL);
				    cout<<"I will print this and die"<<endl;
				    char *strr;
				   		    			strr=new char[req.size()+1];
				   		    			strcpy(strr, req.c_str());
				   		    			cout<<"setting a window settle title"<<endl;
				    gtk_window_set_title (GTK_WINDOW (window),strr);
				    vbox = gtk_vbox_new (FALSE, 5);
				    hbox = gtk_hbox_new (FALSE, 5);
				    gtk_container_add (GTK_CONTAINER (window), hbox);
				    gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
				    gtk_container_set_border_width (GTK_CONTAINER (window), 5);

				    vbox = gtk_vbox_new (FALSE, 5);
				    gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
				    char *str;
				    			str=new char[s.getFilename().size()+1];
				    			strcpy(str, s.getFilename().c_str());
				    frame = gtk_frame_new (str);
						    char *strt;
				   		    			strt=new char[content.size()+1];
				   		    			strcpy(strt, content.c_str());
				    label=gtk_label_new(strt);

				    gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
				    gtk_container_add (GTK_CONTAINER (frame), label);
				    gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);

				    cout<<"show the window now";
				    gtk_widget_show_all (window);

				    gtk_main ();
				    cout<<"cool da.";


	}

};



#endif /* WINDOWELEMENT_H_ */
